package com.bwie.controller;

import com.bwie.pojo.Dishes;
import com.bwie.pojo.EsDishes;
import com.bwie.service.DishesService;
import com.bwie.service.SortService;
import com.bwie.utils.OSSFileUtil;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lenovo
 */
@RestController
@RequestMapping("/dishes")
public class DishesController {

    @Autowired
    private DishesService dishesService;
    @Autowired
    private SortService sortService;
    @Autowired
    private OSSFileUtil ossFileUtil;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    /***
     * @description:创建索引
     * @author: sxting~😊
     * @Date: 2024/10/11 12:22
     * @param:
     * @return:
    **/
    @PostMapping("/createEs")
    public R createEs(){

        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(EsDishes.class);

        if(indexOperations.exists()){
            indexOperations.delete();
        }
        indexOperations.create();

        Document mapping = indexOperations.createMapping();
        indexOperations.putMapping(mapping);

        return R.OK("创建成功");
    }


    /**
     * @description:es列表展示
     * @author: sxting~😊
     * @Date: 2024/10/11 12:23
     * @param:
     * @return:
    **/
    @PostMapping("/list")
    public R list(@RequestBody PageInfoVo pageInfoVo){
        return dishesService.dishesList(pageInfoVo);
    }

    @PostMapping("/sort/list")
    public R list(){
        return sortService.sortList();
    }

    @PostMapping("/upload")
    public R upload(@RequestBody MultipartFile file) throws IOException {
        String s = ossFileUtil.uploadFile(file);
        return R.OK(s);
    }

    @PostMapping("/add")
    public R add(@RequestBody Dishes dishes){
        return dishesService.add(dishes);
    }
}
